<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/dbConnection.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/generalFunctions.php';

class SendMail {
	public static function getEmailTemplate($privacyId, &$template, &$subject) {
		$db = new DbConnection();
		$db->getConnection();
		$query = sprintf("SELECT Template, Subject FROM privacy WHERE PrivacyId = '%u'", $privacyId);
		$result = $db->executeQuery($query);		
		
		if (!$result) {
		    echo 'Could not run query: ' . mysql_error();
		    return;
		}
		
		$row = mysql_fetch_row($result);
		$template = $row[0];
		$subject = $row[1];
		
		mysql_free_result($result);
	}
	
	public static function sendEMail($template, $subject, $params, $to) {
		$matches = array('');
		$i = 0;
		while(count($matches) > 0){
			preg_match("/\[([a-zA-Z0-9])*\]/", $template, $matches);
			if(empty($matches)) {
				break;
			}
			$tag = $matches[0];
			
			$property = substr($tag, 1, strlen($tag)-2);
			if ($property != '') {
				$value = $params[$i];
				$template = str_replace($tag, $value, $template);
				$i++;
			}
		} // while
		
		GeneralFunctions::ubiMail($to, $subject, $template);
	}
}	
?>